package com.magicode.cms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.magicode.cms.common.assist.UtilPath;
import com.magicode.cms.dao.ISpecialTopicDao;
import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.SpecialTopic;
import com.magicode.cms.service.ISpecialTopicService;
@Service("serviceSpecialTopic")
public class SpecialTopicServiceImpl extends BaseServiceImpl<SpecialTopic,ISpecialTopicDao> implements ISpecialTopicService,UtilPath {

	

	@Autowired
	@Qualifier("daoSpecialTopic")
	private ISpecialTopicDao specialTopicDao;

	public ISpecialTopicDao getSpecialTopicDao() {
		return specialTopicDao;
	}

	public void setSpecialTopicDao(ISpecialTopicDao specialTopicDao) {
		this.specialTopicDao = specialTopicDao;
	}

	@Override
	public List<Map<String, Object>> getBeanInfo() {
		List<Map<String, Object>> beaninfo = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(json_key, "title");
		map.put(json_title, "专题标题");
		map.put("width", 150);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "code");
		map.put(json_title, "编码");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "orderCode");
		map.put(json_title, "排序码");
		map.put("width", 100);
		beaninfo.add(map);
		map = new HashMap<String, Object>();
		map.put(json_key, "textContent");
		map.put(json_title, "正文");
		map.put("width", 195);
		beaninfo.add(map);
		return beaninfo;
	}

	@Override
	public String getHql(Map<String, String> querywhere) throws Exception {
		String hql ="from " +SpecialTopic.class.getName()+" "+entity_alias_name+" ";
		if(null!=querywhere && !querywhere.isEmpty()){
			hql += "where 1 = 1  ";  //and "+entity_alias_name+".isContribute = 0
			if(querywhere.containsKey(field_id)){
				hql = hql+" and "+entity_alias_name+".id = '"+querywhere.get(field_id)+"'";
			}
			if(querywhere.containsKey("title")){
				hql = hql+" and "+entity_alias_name+".title like '%"+querywhere.get("title")+"%'";
			}
			if(querywhere.containsKey("text")){
				hql = hql+" and "+entity_alias_name+".text like '%"+querywhere.get("text")+"%'";
			}
		}
		return hql;
	}

	@Override
	public List<Map<String, Object>> grid(Map<String, String> querywhere) {
		List<Map<String, Object>> resultList = null;
		try {
			Integer start = this.getGridStartOrSize(querywhere, 0, field_grid_start);
			Integer size = this.getGridStartOrSize(querywhere, 20, field_grid_size);
			String hql = this.getHql(querywhere);
			Map<String,String> gridsel = new HashMap<String,String>();
			gridsel.put(field_id, ""+entity_alias_name+".id as id ");
			gridsel.put(field_title, ""+entity_alias_name+".title as title ");
			gridsel.put("code", ""+entity_alias_name+".code as code ");
			gridsel.put("orderCode", ""+entity_alias_name+".orderCode as orderCode ");
			gridsel.put("text", ""+entity_alias_name+".text as textContent ");
			
			resultList = this.queryMapDataList(hql, gridsel, start, size);
			resultList = super.convertByteToString(resultList,"textContent");
			return resultList;
		} catch (Exception e) {
			return new ArrayList<Map<String, Object>>();
		}
	}

	@Override
	public ISpecialTopicDao getDao() throws Exception {
		// TODO Auto-generated method stub
		return specialTopicDao;
	}

	
	
}
